<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="工厂" prop="factory">
        <el-select
          v-model="queryParams.factory"
          placeholder="请选择"
          style="width: 240px"
          clearable
          filterable
          @change="handleSearchListChange('factory')"
        >
          <el-option
            v-for="(item, index) in factoryOptions"
            :key="index"
            :label="item.deptName"
            :value="item.deptCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工段名称" prop="workshopCode">
        <el-select
          v-model="queryParams.workshopCode"
          placeholder="请选择"
          clearable
          filterable
          style="width: 240px"
          @change="handleSearchListChange('workshopCode')"
        >
          <el-option
            v-for="(item, index) in workshopOptions"
            :key="index"
            :label="item.deptName"
            :value="item.deptCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产线名称" prop="productLine">
        <el-select
          v-model="queryParams.productLine"
          placeholder="请选择"
          style="width: 240px"
          clearable
          filterable
          @change="handleSearchListChange('productLine')"
        >
          <el-option
            v-for="(item, index) in lineOptions"
            :key="index"
            :label="item.deptName"
            :value="item.deptCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工艺段名称" prop="sectionCode">
        <el-select
          v-model="queryParams.sectionCode"
          placeholder="请选择"
          style="width: 240px"
          clearable
          filterable
          @change="handleSearchListChange('sectionCode')"
        >
          <el-option
            v-for="(item, index) in processOptions"
            :key="index"
            :label="item.deptName"
            :value="item.deptCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="prodName">
        <el-input
          v-model="queryParams.prodName"
          placeholder="请输入产品名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="产出批号" prop="outputBatchNo">
        <el-input
          v-model="queryParams.outputBatchNo"
          placeholder="请输入产出批号"
          clearable
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="质检单号" prop="qmsQualityCode">
        <el-input
          v-model="queryParams.qmsQualityCode"
          placeholder="请输入质检单号"
          clearable
          style="width: 240px"
        />
      </el-form-item>

      <el-form-item label="质检时间段" prop="date">
        <el-date-picker
          v-model="queryParams.date"
          style="width: 240px"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="质检结果" prop="testingResult">
        <el-select
          v-model="queryParams.testingResult"
          placeholder="请选择质检结果"
          clearable
          filterable
          style="width: 240px"
        >
          <el-option
            v-for="(item, index) in quality_testing_result"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          filterable
          style="width: 240px"
        >
          <el-option
            v-for="(item, index) in quality_testing_status"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="typeList" border style="width: 100%" max-height="500px">
      <el-table-column label="生产日期" prop="prodDate" />
      <el-table-column label="班次" prop="className" />
      <el-table-column
        label="班组"
        prop="workGroupName"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column label="订单编号" prop="orderCode" />
      <el-table-column label="收卷时间" prop="rollingTime" />
      <el-table-column label="产出产品" prop="prodName" />
      <el-table-column label="产出批号" prop="outputBatchNo">
        <template #default="scope">
          <el-link type="primary" @click="handleDetailing(scope.row)">
            {{ scope.row.outputBatchNo }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="产出数量" prop="outputQyt" />
      <el-table-column label="缺陷描述" prop="prwDefectDesc" />
      <el-table-column label="请检时间" prop="applyTestingTime" />
      <el-table-column label="质检单号" prop="qmsQualityCode" />
      <el-table-column label="质检结果" prop="testingResult">
        <template #default="scope">
          <dict-tag
            v-if="scope.row.testingResult"
            :options="quality_testing_result"
            :value="scope.row.testingResult"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="外观判定" prop="appearanceJudgment" />
      <el-table-column label="性能判定" prop="performanceJudgment" />
      <el-table-column label="不良描述" prop="defectDesc" />
      <el-table-column label="质检时间" prop="qualityTestingTime" />
      <el-table-column label="工厂" prop="factoryName" />
      <el-table-column label="工段" prop="workshopName" />
      <el-table-column label="产线" prop="productLineName" />
      <el-table-column label="工艺段" prop="sectionName" />
      <el-table-column label="状态" prop="status" fixed="right">
        <template #default="scope">
          <dict-tag :options="quality_testing_status" :value="scope.row.status" />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="productionReport">
import { listProdQualityTesting } from "@/api/productionMgt/qualityRecords";
// getFactoryTree,
import { depeIdSelectList } from "@/api/system/dept";
import DataInfo from "./data";
import { ref, toRefs, reactive, getCurrentInstance, onMounted } from "vue";

const { proxy } = getCurrentInstance();
const typeList = ref([]);
const { quality_testing_status } = proxy.useDict("quality_testing_status");
const { quality_testing_result } = proxy.useDict("quality_testing_result");
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dateRange = ref([]);
var factoryOptions = ref([]);
var workshopOptions = ref([]);
var lineOptions = ref([]);
var processOptions = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    // status: "1",
    name: undefined,
  },
});

const { queryParams } = toRefs(data);
/** 查询设备类型列表 */
function getList() {
  loading.value = true;
  let newForm = JSON.parse(JSON.stringify(queryParams.value));
  if (queryParams.value?.date?.length) {
    newForm.startQualityTestingTime = queryParams.value.date[0];
    newForm.endQualityTestingTime = queryParams.value.date[1];
    delete newForm.date;
  }
  listProdQualityTesting(proxy.addDateRange(newForm)).then((response) => {
    typeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function handleDetailing(row) {
  proxy.subPage(DataInfo, {
    title: "质检详情",
    data: { ...row, parentType: "view" },
  });
}

function getFactorys() {
  // getFactoryTree(query).then((res) => {
  //   factoryOptions.value = res.data;
  // });
  depeIdSelectList({
    deptType: "factory",
    deptCode: "",
    permissionType: "permission",
  }).then((res) => {
    factoryOptions.value = res.data;
  });
}

function handleSearchListChange(type) {
  if (type == "factory") {
    workshopOptions.value = [];
    lineOptions.value = [];
    processOptions.value = [];
    queryParams.value.workshopCode = "";
    queryParams.value.productLine = "";
    queryParams.value.sectionCode = "";
    console.log(queryParams.value.factory, "===queryParams.value.factory");
    if (queryParams.value.factory) {
      depeIdSelectList({
        deptType: "station",
        deptCode: queryParams.value.factory,
        permissionType: "permission",
      }).then((res) => {
        workshopOptions.value = res.data;
        lineOptions.value = [];
        processOptions.value = [];
        queryParams.value.workshopCode = "";
        queryParams.value.productLine = "";
        queryParams.value.sectionCode = "";
      });
    }
  }
  if (type == "workshopCode") {
    lineOptions.value = [];
    processOptions.value = [];
    queryParams.value.productLine = "";
    queryParams.value.sectionCode = "";
    if (queryParams.value.workshopCode) {
      depeIdSelectList({
        deptType: "line",
        deptCode: queryParams.value.workshopCode,
        permissionType: "permission",
      }).then((res) => {
        lineOptions.value = res.data;
        processOptions.value = [];
        queryParams.value.productLine = "";
        queryParams.value.sectionCode = "";
      });
    }
  }
  if (type == "productLine") {
    processOptions.value = [];
    queryParams.value.sectionCode = "";
    if (queryParams.value.productLine) {
      depeIdSelectList({
        deptType: "craft",
        deptCode: queryParams.value.productLine,
        permissionType: "permission",
      }).then((res) => {
        processOptions.value = res.data;
        queryParams.value.sectionCode = "";
      });
    }
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  queryParams.value.status = "";
  handleQuery();
}

getList();
getFactorys();
onMounted(async () => {
  getList();
});
</script>
